package com.shanjiduo.newsline.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStrip;
import com.github.lianghanzhen.LazyFragmentPagerAdapter;
import com.github.lianghanzhen.LazyViewPager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.shanjiduo.newsline.Main2Activity;
import com.shanjiduo.newsline.MainActivity;
import com.shanjiduo.newsline.R;
import com.shanjiduo.newsline.demochannel.ChannelActivity;
import com.shanjiduo.newsline.model.ChannelItem;

/**
 * Created by donglinghao on 2016-01-28.
 */
public class HomeFragment extends Fragment {

    static SlidingFragmentActivity sld;
    private View mRootView;
    private LazyViewPager lvp;
    private PagerSlidingTabStrip pst;
    private TextView add;

    public HomeFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance(SlidingFragmentActivity sld1) {
        HomeFragment fragment = new HomeFragment();
        sld = sld1;
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            Log.e("666", "HomeFragment");
            mRootView = inflater.inflate(R.layout.home_fragment, container, false);
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        initView(mRootView);
        return mRootView;
    }

    private void initView(View view) {
        lvp = (LazyViewPager) view.findViewById(R.id.pager);
        pst = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        lvp.setAdapter(new NewsFragmentPagerAdapter(getChildFragmentManager()));
        add = (TextView) view.findViewById(R.id.home_addView);
        pst.setViewPager(lvp);
        pst.setTextColor(Color.RED);
        pst.setIndicatorColor(Color.BLUE);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), ChannelActivity.class);
                startActivity(intent);
            }
        });
        pst.setIndicatorHeight(5);
        pst.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        sld.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                        break;
                    default:

                        sld.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public class NewsFragmentPagerAdapter extends LazyFragmentPagerAdapter {
        private final String[] TITLES = {"头条", "社会", "军事", "旅游", "教育", "娱乐",
                "体育", "科技", "汽车", "财经"};
        private final String[] NEW_TITLES = {"房产", "电影", "笑话", "游戏", "时尚", "情感", "精选", "电台", "NBA", "彩票", "论坛", "亲子"};
        private final String[] TITLE_ID = {"T1348647909107", "T1348648037603", "T1348648141035", "T1348654204705", "T1348654225495", "T1348648517839", "T1348649079062", "T1348649580692", "T1348654060988", "T1348648756099"};

        public NewsFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * 现在必须实现
         *
         * @param position
         * @return
         */
        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        protected Fragment getItem(ViewGroup container, int position) {

            return NewsFragment.newInstance(TITLES[position], TITLE_ID[position]);
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }
    }
}
